//
//  UIViewController+HDSubmitComController.m
//  app
//
//  Created by D on 15/8/25.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDSubmitComController.h"
#import "HDAddNewComManager.h"
#import "HDNewComSubmitResult.h"
#import "HDMyCommunityController.h"

static const CGFloat kCartTotalPanelHeight = 120;

@interface HDSubmitComController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,MBProgressHUDDelegate>

@property (nonatomic,strong) HDAddNewComManager * addNewComManager;
@property (nonatomic,strong) HDNewComSubmitResult * addNewComResult;
@property (nonatomic,strong) UITableView * submitTableView;
@property (nonatomic,strong) UITextField * smsFld;
@property (nonatomic,strong) UIButton * sendBtn;
@property (strong,nonatomic) MBProgressHUD* progress_;

@end

@implementation HDSubmitComController

-(void)viewDidLoad
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.submitTableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 28),
                                                                      [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight oh:+56]) style:UITableViewStyleGrouped];
    
    
   
    self.submitTableView.backgroundColor = [UIColor whiteColor];
    self.submitTableView.delegate = self;
    self.submitTableView.dataSource = self;
    
    [self.view addSubview:self.submitTableView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard_com:)];
    [self.submitTableView addGestureRecognizer:tap];
    
    
    self.sendBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ox:-20 fy:UB_TOP],
                                                            [self.ubo ow:40 oh:40])];
    
    [self.sendBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sendBtn setTitleColor:[XCommon hexStringToColor:kColor_red_text] forState:UIControlStateNormal];
    [self.sendBtn.titleLabel setFont:JKAppGlobleFontSmallTagText];
    self.sendBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.sendBtn addTarget:self action:@selector(submitClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.sendBtn];
    
    self.addNewComManager = [HDAddNewComManager shareManager];
    self.addNewComResult = [[HDNewComSubmitResult alloc]initWithTarget:self action:@selector(sumbitEndResult:)];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"完成";
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.smsFld resignFirstResponder];
}

-(void)submitClicked:(UIButton*)sender
{
    if (self.smsFld.text.length > 0) {
        [self.smsFld resignFirstResponder];
        [self submitResult];

    }
    
}


#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.smsFld = [[UITextField alloc]initWithFrame:CGRectOf([cell.ubo fx:0 ox:12 fy:UB_1_2 oy:-22], [cell.ubo ow:UBO_WINDOW.width-24 oh:44])];
        self.smsFld.delegate = self;
        self.smsFld.placeholder = @"请输入验证码";
        [self.smsFld setTextColor:[XCommon hexStringToColor:kColor_gray_text]];
        [cell addSubview:self.smsFld];

    }

    cell.backgroundView = nil;
    
    return cell;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        //        DLog(@"长度 = %lu",(unsigned long)self.toolBarView.messageContent.text.length);
        
        if (self.smsFld.text.length > 0) {
            
             [self.smsFld resignFirstResponder];
            [self submitResult];
            return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
            
        }else
        {
            UIAlertView * av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [av show];
        }
        
        
    }
    
    return YES;
}
-(void)submitResult
{
    self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.progress_];
    [self.view bringSubviewToFront:self.progress_];
    self.progress_.delegate = self;
    [self.progress_ show:YES];
    
    NSDictionary * params = @{@"smscode":self.smsFld.text,
                              @"cmid":[[[NSUserDefaults standardUserDefaults]objectForKey:@"house"]objectForKey:@"community_id"],
                              @"id":[[[NSUserDefaults standardUserDefaults]objectForKey:@"house"]objectForKey:@"id"],
                              @"selected":@"1"};
    
    NSDictionary * newComDic = @{@"cmid":[[[NSUserDefaults standardUserDefaults]objectForKey:@"house"]objectForKey:@"community_id"],
                                 @"id":[[[NSUserDefaults standardUserDefaults]objectForKey:@"house"]objectForKey:@"id"],
                                 @"selected":@"1",
                                 @"community_name":[[[NSUserDefaults standardUserDefaults]objectForKey:@"community"]objectForKey:@"community_name"],
                                 @"house_name":[[[NSUserDefaults standardUserDefaults]objectForKey:@"house"]objectForKey:@"house_name"]};
    
    [self.addNewComManager submitEnd:self.addNewComResult Params:params NewComDic:newComDic];
}

-(void)sumbitEndResult:(HDAddNewComResult*)rt
{
    
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    
    if (rt.success) {
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"community"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"community_zu"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"building"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"house"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"comPhone"];
        
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[HDMyCommunityController class]]) {
                [self.navigationController popToViewController:temp animated:YES];
                
                //在这里发送通知
                [[NSNotificationCenter defaultCenter]postNotificationName:@"addNewCommunity" object:nil];
                
                
            }
        }
        
    }

}
#pragma mark - 点击隐藏键盘
-(void)hideKeyboard_com:(UITapGestureRecognizer*)tap
{
    [self.smsFld resignFirstResponder];
}

@end














